#!/bin/sh

set -e

# Check if it is first installation or not
if [ ! -f "artisan" ]; then
    # Clear everything in order to prevent error "Project directory is not empty"
    find . -mindepth 1 -delete

    # Install Laravel app
    composer create-project laravel/laravel . --no-scripts

    # Copy .env settings
    cp /app/.env.example .env

    # Generate app keys
    php artisan key:generate

    # Repopulate .gitkeep
    # NOTE: this is for development of this image purposes only
    # when it comes to deploy the image there should be workaround...
    touch .gitkeep
fi

# Install Laravel Octane
if [ ! -f "frankenphp" ]; then
    composer require laravel/octane
    php artisan octane:install --server=frankenphp --no-interaction
fi

# Build assets
bun install && bun run build

# Install Chokidar (required by Octane)
bun add --dev chokidar

# Run app
php artisan octane:start \
    --server=frankenphp \
    --port=80 \
    --host=localhost \
    --watch \
    --admin-port=2019
